package com.itwlj.mapper;

import com.itwlj.entity.Sku;
import com.itwlj.entity.SkuSpec;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface SkuMapper {

    /**
     * 新增sku
     *
     * @param sku
     */
    void insert(Sku sku);

    /**
     * 新增sku规格
     *
     * @param skuSpec
     */
    @Insert("insert into sku_spec(sku_id, name, value_name) values " +
            "(#{skuId},#{name},#{valueName})")
    void insertSpec(SkuSpec skuSpec);

    /**
     * 根据商品id查询sku
     *
     * @param goodsId
     * @return
     */
    @Select("select id, goods_id, picture, sku_code, price, inventory from sku where goods_id = #{goodsId}")
    List<Sku> getByGoodId(Long goodsId);

    /**
     * 根据sku id查询sku规格
     *
     * @param skuId
     * @return
     */
    @Select("select id, name, value_name from sku_spec where sku_id = #{skuId}")
    List<SkuSpec> getSkuSpecs(Long skuId);

    /**
     * 根据id删除sku
     *
     * @param id
     */
    @Delete("delete from sku where id = #{id}")
    void deleteById(Long id);

    /**
     * 根据sku id删除sku规格
     *
     * @param skuId
     */
    @Delete("delete from sku_spec where sku_id = #{skuId}")
    void deleteSpec(Long skuId);

    /**
     * 根据id修改sku
     *
     * @param sku
     */
    @Update("update sku set " +
            "picture = #{picture}, sku_code = #{skuCode}, price = #{price},inventory = #{inventory} where id = #{id}")
    void update(Sku sku);

    /**
     * 根据id查询sku
     *
     * @param id
     * @return
     */
    @Select("select id, goods_id, picture, sku_code, price, inventory from sku where id = #{id}")
    Sku getById(Long id);

    /**
     * 扣减库存
     *
     * @param id
     * @param count
     */
    @Update("update sku set inventory = inventory - #{count} where id = #{id}")
    void deductInventory(Long id, Long count);

    /**
     * 回滚库存
     *
     * @param id
     * @param count
     */
    @Update("update sku set inventory = inventory + #{count} where id = #{id}")
    void backInventory(Long id, Long count);
}
